package com.lightboat.common.utils;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.lightboat.common.enums.RedisSnEnum;
import com.lightboat.common.exception.ServiceException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * @author 小政同学 it_xiaozheng@163.com
 * @date 2023-08-02 9:39
 */
@Component
@AllArgsConstructor
public class RedisSnGenerator {

    private final RedisUtils redisUtils;

    private final String key = "serialNumber:sn";

    /**
     * 获取指定业务订单编号
     *
     * @param busId 业务ID null 默认为b00
     * @param step  步长
     * @return
     */
    public String getSn(String busId, Long step) {
        if (step == 0) {
            throw new ServiceException("请指定订单编号步长");
        }
        busId = ObjUtil.isNull(busId) ? RedisSnEnum.DEFAULT.getKey() : busId;
        RedisSnEnum snEnum = RedisSnEnum.findKey(busId);
        switch (snEnum){
            case ACCOUNT:
            {
                Long increment = redisUtils.hincr(key, busId, step) + RedisSnEnum.ACCOUNT.getInitial();
                return String.format("%06d", increment);
            }
            case REPURCHASE_ORDER:
            case AFTER_SALE_ORDER:
            case ORDER:
            {
                String YYYMMDD = LocalDate.now().format(DateUtils.DFYMD);
                String idKey = String.format("%1s:%2s", busId, YYYMMDD);
                Long increment = redisUtils.hincr(key, idKey, step);
                return String.format("%s%06d", YYYMMDD, increment);
            }
            case WITHDRAWAL:
            {
                String YYYMMDD = LocalDate.now().format(DateUtils.DFYMD);
                String idKey = String.format("%1s:%2s", busId, YYYMMDD);
                Long increment = redisUtils.hincr(key, idKey, step);
                return String.format("%s%s%06d", snEnum.getCode(), YYYMMDD, increment);
            }
            default:
                return  redisUtils.hincr(key, RedisSnEnum.DEFAULT.getKey(), step).toString();
            }
    }



    public String getSn(String busId,String keyVal,Long step) {
        if (step == 0) {
            throw new ServiceException("请指定订单编号步长");
        }
        busId = ObjUtil.isNull(busId) ? RedisSnEnum.DEFAULT.getKey() : busId;
        RedisSnEnum snEnum = RedisSnEnum.findKey(busId);
        switch (snEnum){
            case ACCOUNT:
            {
                Long increment = redisUtils.hincr(key, busId, step) + RedisSnEnum.ACCOUNT.getInitial();
                return String.format("%06d", increment);
            }
            case REPURCHASE_ORDER:
            case AFTER_SALE_ORDER:
            case ORDER:
            {
                String idKey = String.format("%1s:%2s", busId, keyVal);
                Long increment = redisUtils.hincr(key, idKey, step);
                return String.format("%s%06d", snEnum.getCode(), increment);
            }
            case WITHDRAWAL:
            {
                String idKey = String.format("%1s:%2s", busId, keyVal);
                Long increment = redisUtils.hincr(key, idKey, step);
                return String.format("%s%s%06d", snEnum.getCode(), keyVal, increment);
            }
            default:
                return  redisUtils.hincr(key, RedisSnEnum.DEFAULT.getKey(), step).toString();
        }
    }

    /**
     * 获取指定业务订单编号
     *
     * @param busId 业务ID null 默认为b00
     * @return
     */
    public String getBusSn(RedisSnEnum busId) {
        String sn = this.getSn(busId);
        return busId.getCode() + sn;
    }

    /**
     * 获取指定业务订单编号，默认步长为1
     *
     * @return
     */
    public String getSn(RedisSnEnum busId) {
        return this.getSn(busId.getKey(), 1L);
    }

    /**
     * 获取业务订单编号，默认b00，默认步长为1
     *
     * @return
     */
    public String getSn() {
        return this.getSn(null, 1L);
    }

    public static RedisSnGenerator build(){
        return SpringUtil.getBean(RedisSnGenerator.class);
    }

}
